using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Forecast.Business;
using Forecast.Database;

public partial class controls_grids_PredictionResults : System.Web.UI.UserControl
{
    public void BindData(int predictionId)
    {
        ViewState["archivePredictionId"] = predictionId;
        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        DataTable table = activity.GetPredictionResults(predictionId);
        DataView view = new DataView(table);
        view.Sort = "[" + PredictionResults.Columns.Rank.ToString() + "] ASC";

        GridView1.DataSource = view;
        GridView1.DataBind();

        if (GridView1.Rows.Count == 0)
        {
            _panel.Visible = false;
            
        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataView view = GridView1.DataSource as DataView;

        if (ViewState["sortColumn"].ToString() == e.SortExpression.ToString())
        {
            if ("DESC" == ViewState["sortDirection"].ToString())
            {
                ViewState["sortDirection"] = "ASC";
            }
            else
            {
                ViewState["sortDirection"] = "DESC";
            }
        }
        else
        {
            ViewState["sortColumn"] = e.SortExpression.ToString();
            ViewState["sortDirection"] = "DESC";
        }

        view.Sort = ViewState["sortColumn"].ToString() + " " + ViewState["sortDirection"].ToString();

        GridView1.DataSource = view;
        GridView1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ViewState["sortColumn"] = " ";
            ViewState["sortDirection"] = " ";
        }

        if (ViewState["archivePredictionId"] != null)
        {
            int predictionId = Int32.Parse(ViewState["archivePredictionId"].ToString());
            BindData(predictionId);
        }
    }
}
